<?php

class ItemModel extends DBModel
{
	protected $_table = '{{item}}';
	protected $_id = 'itemid';
	
	public function getListIn($itemid, $select = '*')
	{
		if ($itemid == '') {
			return false;
		}
		$where['select'] = $select;
		$where['condition'] = 'itemid IN ('. $itemid .') and status = 1';
		$where['order'] = 'orderid DESC,itemid DESC';
		return $this->findAll($where);
	}
	
	public function getList($pageno, $pagesize, $itemtype = false, $itemid = false, $select = '*', $itemname = '', $isshopping = false)
	{
		$where['select'] = $select;
		$where['order'] = 'orderid DESC,itemid DESC';
		$where['limit'] = $pagesize;
		$where['offset'] = max(0, $pageno - 1) * $pagesize;
		$params = array();
		$where['condition'] = 'status = 1';
		if ($itemtype) {
			$where['condition'] .= ' AND itemtype = :itemtype';
			$params[':itemtype'] = $itemtype;
		} else {
			$where['condition'] .= ' AND itemtype IN ('. ITEM_TYPE_MENU .', '. ITEM_TYPE_MOVIE .', '. ITEM_TYPE_PHOTO .', '. ITEM_TYPE_SCHOOL .')';
		}
		if ($itemid) {
			$where['condition'] .= ' AND itemid IN('. $itemid .')';
		}
		if (!empty($itemname)) {
			$where['condition'] .= ' AND title LIKE :title';
			$params[':title'] = '%'. $itemname .'%';
		}
		if ($isshopping !== false) {
			if ($isshopping == 1) {
				$where['condition'] .= ' AND howmuch is not null';
			} else {
				$where['condition'] .= ' AND howmuch is null';
			}
		}
	
		return $this->findAll($where, $params);
	}
	
	public function getMyList($pageno, $pagesize, $itemtype = false, $itemid = false, $select = '*', $itemname = '', $isshopping = false)
	{
		$where['select'] = $select;
		$where['order'] = 'orderid DESC,itemid DESC';
		$where['limit'] = $pagesize;
		$where['offset'] = max(0, $pageno - 1) * $pagesize;
		$params = array();
		$where['condition'] = '1=1';
		if ($itemtype) {
			$where['condition'] .= ' AND itemtype = :itemtype';
			$params[':itemtype'] = $itemtype;
		} else {
			$where['condition'] .= ' AND itemtype IN ('. ITEM_TYPE_MENU .', '. ITEM_TYPE_MOVIE .', '. ITEM_TYPE_PHOTO .', '. ITEM_TYPE_SCHOOL .')';
		}
		if ($itemid) {
			$where['condition'] .= ' AND itemid IN('. $itemid .')';
		}
		if (!empty($itemname)) {
			$where['condition'] .= ' AND title LIKE :title';
			$params[':title'] = '%'. $itemname .'%';
		}
		if ($isshopping !== false) {
			if ($isshopping == 1) {
				$where['condition'] .= ' AND howmuch is not null';
			} else {
				$where['condition'] .= ' AND howmuch is null';
			}
		}
	
		return $this->findAll($where, $params);
	}
	
	public function getList1($pageno, $pagesize, $itemtype = false, $itemid = false, $select = '*', $itemname = '', $priority = 1)
	{
		$where['select'] = $select;
		$where['order'] = 'orderid DESC,itemid DESC';
		$where['limit'] = $pagesize;
		$where['offset'] = max(0, $pageno - 1) * $pagesize;
		$params = array();
		$where['condition'] = 'status = 1';
		$where['condition'] .= ' AND priority <= :priority';
		$params[':priority'] = $priority;
		if ($itemtype) {
			$where['condition'] .= ' AND itemtype = :itemtype';
			$params[':itemtype'] = $itemtype;
		} else {
			$where['condition'] .= ' AND itemtype in ('.ITEM_TYPE_TRENDS.', '. ITEM_TYPE_ARTICLE .')';
		}
		if ($itemid) {
			$where['condition'] .= ' AND itemid IN('. $itemid .')';
		}
		if (!empty($itemname)) {
			$where['condition'] .= ' AND title LIKE :title';
			$params[':title'] = '%'. $itemname .'%';
		}
	
		return $this->findAll($where, $params);
	}
	
	/**
	 * 是否第一次发动态
	 * @param unknown $userid
	 */
	public function firstFeed($userid, $howmuch)
	{
		$itemid = $this->model('ItemUser')->getItemIdByUserID($userid);
		if (!$itemid) {
			$data = array(
				'uid' => $userid,
				'paytype' => '赠送第一次发布动态',
				'howmuch' => $howmuch,
				'ordernum' => time(),
				'createtime' => time()
			);
			if ($this->model('Financedetail')->insert($data) !== false) {
				$this->model('UserMoney')->addMoney($userid, $howmuch);
			}
		} else {
			$where['select'] = 'itemid';
			$where['condition'] = "itemid in (". $itemid .") and itemtype = " . ITEM_TYPE_TRENDS;
			if (!$this->find($where)) {
				$data = array(
					'uid' => $userid,
					'paytype' => '赠送第一次发布动态',
					'howmuch' => $howmuch,
					'ordernum' => time(),
					'createtime' => time()
				);
				if ($this->model('Financedetail')->insert($data) !== false) {
					$this->model('UserMoney')->addMoney($userid, $howmuch);
				}
			}
		}
	}
	
	public function addTrends($titlelogo, $content, $userid, $priority)
	{
		$trends = array(
			'titlelogo' => $titlelogo,
			'remark' => $content,
			'createtime' => time(),
			'priority' => $priority,
			'itemtype' => ITEM_TYPE_TRENDS
		);
		$this->firstFeed($userid, 38);
		if ($this->insert($trends)) {
			return true;
		}
		return false;
	}
	
	public function addArticle($title, $titlelogo, $content, $priority)
	{
		$trends = array(
			'title' => $title,
			'titlelogo' => $titlelogo,
			'remark' => $content,
			'createtime' => time(),
			'priority' => $priority,
			'itemtype' => ITEM_TYPE_ARTICLE
		);
		return $this->insert($trends);
	}
	
	public function addMoney($itemid, $howmuch)
	{
		return $this->queryBySql('update ' . $this->getTable() . ' set  supportmoney = supportmoney + '. $howmuch . ' where itemid = ' . $itemid);
	}
	
	public function getLastFeed($userid, $select = '*')
	{
		$where['condition'] = 'i.itemtype = ' . ITEM_TYPE_TRENDS . ' and u.userid = ' . $userid;
		$where['from'] = '{{item}} i';
		$where['select'] = $select;
		$where['join'] = 'left join {{item_user}} iu on iu.itemid = i.itemid left join {{user}} u on u.userid = iu.userid';
		$where['order'] = 'i.createtime DESC';
		return $this->find($where);
	}
	
	public function getFeedAll($userids, $select = '*', $pageno = 1, $pagesize = 10, $priority = 2)
	{
		$where['condition'] = 'i.itemtype = ' . ITEM_TYPE_TRENDS . ' and priority <= '. $priority .' and u.userid in ('. $userids .')';
		$where['from'] = '{{item}} i';
		$where['select'] = $select;
		$where['join'] = 'left join {{item_user}} iu on iu.itemid = i.itemid 
						  left join {{user}} u on u.userid = iu.userid 
						  left join {{photo}} p on u.logoid = p.photoid';
		$where['order'] = 'i.createtime DESC';
		$where['limit'] = $pagesize;
		$where['offset'] = max(0, $pageno - 1) * $pagesize;
		return $this->findAll($where);
	}
	
	public function getHowmuch($itemid)
	{
		$where['select'] = 'howmuch';
		$where['condition'] = 'itemid = :itemid';
		$params[':itemid'] = $itemid;
		return (int) $this->findScalar($where, $params);
	}
	
	public function addVisit($itemid)
	{
		$sql = 'update {{item}} set visitnum = visitnum + 1 where itemid = :itemid';
		return $this->queryBySql($sql, array(':itemid' => $itemid));
	}
}